$(function () {
    var flag=0; //删除键的效果切换 0为初始值
    // var contentChooseFlag=0; //内容模块来回切换是否选中
    var myDate= new Date();
    var startYear=myDate.getFullYear()-50;//起始年份
    var endYear=myDate.getFullYear()+50;//结束年份
    var yearSelect=document.getElementById('choseYear_projectManage');
    for (var i=startYear;i<=endYear;i++)
    {
        var option=document.createElement("option");
        option.text=" "+i+'年';
        try{
            // 对于更早的版本IE8
            yearSelect.add(option,yearSelect.options[null]);
        }catch (e){
            yearSelect.add(option,null);
        }
        // yearSelect.options.add(new Option(i,i)+'年');
    }
    yearSelect.options[yearSelect.options.length-51].selected=1;

    //标签页切换操作
    var tab=document.getElementsByClassName('tabNavigation')[0];
    EventUtil.addHandler(tab,"click",function (event) {
        event=EventUtil.getEvent(event);
        var target=EventUtil.getTarget(event);
        // console.log($('.projectALLContent').hasClass('active'))
        switch (target.id){
            case "itemAll":
                if(!$(target).parent().hasClass('active')){
                    $(target).parent().siblings().removeClass('active');
                    $(target).parent().addClass('active');
                }
                if(!$('.projectALLContent').hasClass('active')){
                    $('.projectALLContent').addClass('active');
                    $('.projectALLContent').siblings().removeClass('active');
                }
                break;
            case "itemEstablished":
                // console.log(1)
                // console.log($(target).parent())
                if(!$(target).parent().hasClass('active')){
                    $(target).parent().siblings().removeClass('active');
                    $(target).parent().addClass('active');
                }
                if(!$('.projectEstablishedContent').hasClass('active')){
                    $('.projectEstablishedContent').addClass('active');
                    $('.projectEstablishedContent').siblings().removeClass('active');
                }
                break;
            case "itemFinished":
                if(!$(target).parent().hasClass('active')){
                    // console.log(1)
                    // console.log($(target).parent());
                    $(target).parent().siblings().removeClass('active');
                    $(target).parent().addClass('active');
                    // console.log($(target).parent());
                }
                if(!$('.projectFinishedContent').hasClass('active')){
                    $('.projectFinishedContent').addClass('active');
                    $('.projectFinishedContent').siblings().removeClass('active');
                }
                break;
        }
    })

    //删除点击事件 0 红框 可点击 按钮变蓝 1  删除列表 按钮变白
    var btnDelete=document.getElementsByClassName('deleteOne')[0];
    EventUtil.addHandler(btnDelete,"click",function (event) {
        event = EventUtil.getEvent(event);
        var target = EventUtil.getTarget(event);
        // console.log(target)
        if(!$(target).hasClass('active')){
            // console.log(1)
            $('.itemContent').css("cursor","pointer");
            //是否将查看 导出文件等按钮去掉cursor
            // $('.itemContent').removeClass('boxShadow').addClass('redBoxShadow');
            // $('.partContent').css("box-shadow","0px 0px 1px 3px rgb(230,5,20)");
            // $(target).css('background-color','#00A0E9').css('color','#FFFFFF');
            $(target).addClass('active');
            // console.log(target)

        }
        else if($(target).hasClass('active')){
            // $('.itemContent').css("border","1px solid #FFFFFF");
            // console.log(2)
            $('.itemContent').css("cursor","default");
            // $('.itemContent').removeClass('redBoxShadow').addClass('boxShadow');
            // $('.partContent').css("box-shadow","0px 0px 2px 4px rgb(235,235,235)");
            $(target).removeClass('active');
            // console.log(target)

            //删除灰色背景的li
            //
            // console.log($('.shadePane.active').parent().attr('data-index'))
            let delete_list = $('.shadePane.active').parent()
            let delete_arr = []
            for( let i = 0; i < delete_list.length; i++){
                delete_arr.push($(delete_list[i]).attr('data-index'))
            }
            // console.log(delete_arr)
            $.ajax({
                url:'http://he.youledi.cn/Admin/Project/deleteProj',
                type:'post',
                data:{
                    pro_id: delete_arr
                },
                success:function(data){
                    "use strict";
                    if(data.code==1){
                        $('.shadePane.active').parent().remove();
                    }
                }
            })
        }
    });

    //点击变灰
    var actionList=document.getElementsByClassName('projectContent active')[0];
    // console.log(actionList);
    EventUtil.addHandler(actionList,"click",function (event) {
        event=EventUtil.getEvent(event);
        var target=EventUtil.getTarget(event);
        var current=$(target).parents('.itemContent').children('.shadePane');
        // console.log($(target).hasClass('active'))
        if($('.deleteOne').hasClass('active')){
            if(!current.hasClass('active')){
                current.addClass('active');
            }else{
                current.removeClass('active');
            }
        }

        // console.log($(target).parents('.itemContent'));
    })

    $('body').on('click','.exportFile',function(e){
        e.preventDefault();
        // console.log($(this).attr('data-id'))
        let pro_id = $(this).attr('data-id')
        $.ajax({
            url:'http://he.youledi.cn/Admin/Project/getProYear',
            type:'get',
            data:{
                id:pro_id
            },
            success:function(data){
                if(data.code==1){
                    let datas = data.data
                    let html = ''
                    datas.forEach(key => {
                        // console.log(key)
                        html+=`<li>${key}</li>`
                    })
                    $('.choose_list').html(html)
                    window.sessionStorage['pro_id'] = pro_id
                    $('#choose').addClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
            }
        })
    })

    $('.choose_list').on('click','li',function(e){
        window.sessionStorage['choose_year'] = $(this).html()
        $(this).addClass('active').siblings().removeClass('active')
    })

    $('.choose_btn').click(function(e){
        //防止重复点击按钮
        if( $(this).attr('lock') ) return
        
        let choose_year  = window.sessionStorage['choose_year']
        console.log(choose_year)
        if(!choose_year){
            $('#yz_modal').html('时间一定要选择！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        
        $(this).attr('lock','lock')

        if($('#choose').find('.choose_btn').attr('export')=='huizong'){
            window.open(`http://he.youledi.cn/Admin/Project/exportTotal?year=${choose_year}`,"_blank");

            $('#choose').removeClass('active');
            $(this).removeAttr('lock')
            window.sessionStorage['choose_year'] = ''
        }else if($('#choose').find('.choose_btn').attr('export')=='guiji'){
            // window.open(`http://he.youledi.cn/Admin/Project/exportTotal?year=${choose_year}`,"_blank");

            $('#choose').removeClass('active');
            $(this).removeAttr('lock')
            window.sessionStorage['choose_year'] = ''
        }else{
            let pro_id  = window.sessionStorage['pro_id']
            
            $(this).removeAttr('lock')
            window.sessionStorage['pro_id'] = ''
            window.sessionStorage['choose_year'] = ''
            window.open(`http://he.youledi.cn/Admin/Project/exportPro?id=${pro_id}&year=${choose_year}`,"_blank");
        }




    })


    $('#export_huizong').click(function(){
        let date = new Date()
        let year = date.getFullYear()
        let year_1 = date.getFullYear()-1
        let year_2 = date.getFullYear()-2
        let html = `<li>${year}</li><li>${year_1}</li><li>${year_2}</li>`
        $('#choose').find('.choose_list').html(html)
        $('#choose').addClass('active')

        $('#choose').find('.choose_btn').attr('export','huizong')
        // $('#choose').find('.choose_btn').attr('lock','lock')
    })
    $('#export_guiji').click(function(){
        let date = new Date()
        let year = date.getFullYear()
        let year_1 = date.getFullYear()-1
        let year_2 = date.getFullYear()-2
        let html = `<li>${year}</li><li>${year_1}</li><li>${year_2}</li>`
        $('#choose').find('.choose_list').html(html)
        $('#choose').addClass('active')

        $('#choose').find('.choose_btn').attr('export','guiji')
        // $('#choose').find('.choose_btn').attr('lock','lock')
    })

    //当点击区域不在导入弹框中时则关闭弹框
    $(document).click(function(e){
        let $this = $(e.target);
        if($this.attr('id') == 'choose' && $this.parent().attr('id') != 'choose'){
            $('#choose').removeClass('active');
            window.sessionStorage['pro_id'] = ''
            window.sessionStorage['choose_year'] = ''
        }
    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }

})